﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.UC.销售记录.service;
using DXApplicationTangche.UC.门店出入库.Service;
using mendian;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.订单一览新.销售确认单
{
    public partial class Form现货退货 : DevExpress.XtraEditors.XtraForm
    {
        private List<SalesAssistantDTO> salesAssistantDTOs = new List<SalesAssistantDTO>();
        private ReturnVerifyVO returnVerifyVO = new ReturnVerifyVO();
        private String quoteId { get; set; }
        public Form现货退货()
        {
            InitializeComponent();
            this.searchLookUpEdit店铺.Properties.DataSource = 销售记录Service.get店铺();
            this.config读取店铺();
            this.salesAssistantDTOs = 销售确认Service.get销售员带助记码();
            this.searchLookUpEdit销售员.Properties.DataSource = this.salesAssistantDTOs;
            this.searchLookUpEdit辅销售.Properties.DataSource = 销售记录Service.get销售员();
        }

        private void textBox成衣扫码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                //List<RefSaleRecordDTO> refSaleRecordDTOs = new List<RefSaleRecordDTO>();
                //RefSaleRecordDTO refSaleRecordDTO = 销售确认Service.getSaleReordByBarcode(this.textBox成衣扫码.Text.Trim());
                //refSaleRecordDTOs.Add(refSaleRecordDTO);
                //this.gridControl订单.DataSource = refSaleRecordDTOs;
                //this.quoteId = refSaleRecordDTO.id;
                SaleConfirmQueryWholeDTO saleConfirmQueryWhole = 销售确认Service.getSaleConfirmWholeByBarcode(this.textBox成衣扫码.Text.Trim());
                this.显示(saleConfirmQueryWhole);
            }
        }
        private void 显示(SaleConfirmQueryWholeDTO saleConfirmQueryWhole)
        {
            List<SaleConfirmQueryWholeDTO> saleConfirmQueryWholeDTOs = new List<SaleConfirmQueryWholeDTO>();
            saleConfirmQueryWholeDTOs.Add(saleConfirmQueryWhole);
            this.gridControl订单.DataSource = saleConfirmQueryWholeDTOs;
            this.quoteId = saleConfirmQueryWhole.id;
            //
            this.returnVerifyVO
                .buildAssistantIdName(saleConfirmQueryWhole.assistant, saleConfirmQueryWhole.assistantName)
                .buildSales(saleConfirmQueryWhole.sales, saleConfirmQueryWhole.salesName);
            this.searchLookUpEdit销售员.Text = this.returnVerifyVO.assistantName;
            this.searchLookUpEdit辅销售.Text = this.returnVerifyVO.salesName;
            //  paymentType
            if ("账户余额支付".Equals(saleConfirmQueryWhole.paymentType))
            {
                this.checkBox是否退回账户余额.Checked = true;
                this.checkBox是否退回账户余额.Enabled = false;
            }
            else
            {
                this.checkBox是否退回账户余额.Checked = false;
                this.checkBox是否退回账户余额.Enabled = false;
            }
            //  主销售
            if (!String.IsNullOrEmpty(saleConfirmQueryWhole.assistant))
            {
                foreach (SalesAssistantDTO item in this.salesAssistantDTOs)
                {
                    if (saleConfirmQueryWhole.assistant.Equals(item.id))
                    {
                        this.returnVerifyVO.buildAssistantMnemonic(item.mnemonic);
                        break;
                    }
                }
                this.searchLookUpEdit销售员.Enabled = false;
            }
            else
            {
                this.searchLookUpEdit销售员.Enabled = true;
            }
            //  辅销售
            if (!String.IsNullOrEmpty(saleConfirmQueryWhole.sales))
            {
                this.searchLookUpEdit辅销售.Enabled = false;
            }
            else
            {
                this.searchLookUpEdit辅销售.Enabled = true;
            }
        }
        #region
        #region 店铺
        private void searchLookUpEdit店铺_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear店铺Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit店铺View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit店铺.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    String storeId = this.searchLookUpEdit店铺.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    String storeName = this.searchLookUpEdit店铺.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                    this.returnVerifyVO.buildStore(storeId, storeName);
                    if (!System.IO.File.Exists(@"xml\config店铺.xml"))
                    {
                        DealXML.ObjectToXMLFile(new DTO销售表头(storeId, storeName), @"xml\config店铺.xml", Encoding.UTF8);
                    }
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear店铺Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit店铺.ToolTip = null;
            searchLookUpEdit店铺.EditValue = null;
        }
        private void searchLookUpEdit店铺_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.returnVerifyVO.storeName;
        }
        private void config读取店铺()
        {
            if (System.IO.File.Exists(@"xml\config店铺.xml"))
            {
                DTO销售表头 dTO店铺 = DealXML.XMLFlieToObject<DTO销售表头>(@"xml\config店铺.xml", Encoding.UTF8);
                this.returnVerifyVO.buildStore(dTO店铺.id, dTO店铺.name);
                this.searchLookUpEdit店铺.Text = this.returnVerifyVO.storeName;
                this.searchLookUpEdit店铺.Enabled = false;
            }
        }
        private void simpleButton重选店铺_Click(object sender, EventArgs e)
        {
            if (System.IO.File.Exists(@"xml\config店铺.xml"))
            {
                File.Delete(@"xml\config店铺.xml");
                this.searchLookUpEdit店铺.Enabled = true;
                this.returnVerifyVO.clearStore();
                this.searchLookUpEdit店铺.Text = "";
            }
        }
        #endregion
        #region 销售员
        private void searchLookUpEdit销售员_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear销售员Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit销售员View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit销售员.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    String assistantId = this.searchLookUpEdit销售员.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    String assistantName = this.searchLookUpEdit销售员.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                    String assistantMnemonic = this.searchLookUpEdit销售员.Properties.View.GetRowCellValue(rowHandle, "mnemonic").ToString();
                    this.returnVerifyVO.buildAssistant(assistantId, assistantName, assistantMnemonic);
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear销售员Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit销售员.ToolTip = null;
            searchLookUpEdit销售员.EditValue = null;
        }
        private void searchLookUpEdit销售员_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.returnVerifyVO.assistantName;
        }
        #endregion
        #region 辅销售
        private void searchLookUpEdit辅销售_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear成单人Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit辅销售View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit辅销售.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    String salesId = this.searchLookUpEdit辅销售.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    String salesName = this.searchLookUpEdit辅销售.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                    this.returnVerifyVO.buildSales(salesId, salesName);
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear成单人Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit辅销售.ToolTip = null;
            searchLookUpEdit辅销售.EditValue = null;
        }
        private void searchLookUpEdit辅销售_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.returnVerifyVO.salesName;
        }
        #endregion
        #endregion


        private void simpleButton退货_Click(object sender, EventArgs e)
        {
            if (!this.returnVerifyVO.build判空())
            {
                MessageBox.Show("请选择店铺及销售员");
                return;
            }
            if (String.IsNullOrEmpty(this.returnVerifyVO.assistantMnemonic))
            {
                MessageBox.Show("请选择主销售员");
                this.searchLookUpEdit销售员.Enabled = true;
                return;
            }
            //更新退货
            销售确认Service.saleReturnByBarcodeWithSales(
                    this.quoteId,
                    this.textBox成衣扫码.Text.Trim(),
                    this.returnVerifyVO.storeId,
                    this.returnVerifyVO.assistantId,
                    String.IsNullOrEmpty(this.returnVerifyVO.salesId) ? this.returnVerifyVO.assistantId : this.returnVerifyVO.salesId,
                    this.checkBox是否退回账户余额.Checked ? "账户余额支付" : "现金支付",
                    this.textBox备注.Text.Trim()
                );
            //回库
            出入库Service.saveStockSaleReturnIn(
                            this.textBox成衣扫码.Text.Trim(),
                            this.returnVerifyVO.storeId,
                            this.returnVerifyVO.storeName,
                            this.returnVerifyVO.assistantMnemonic
                            );
            this.Close();
        }
    }
    public class ReturnVerifyVO
    {
        public String storeId { get; set; }
        public String storeName { get; set; }
        public String assistantId { get; set; }
        public String assistantName { get; set; }
        public String assistantMnemonic { get; set; }
        public String salesId { get; set; }
        public String salesName { get; set; }
        public ReturnVerifyVO()
        {

        }

        public ReturnVerifyVO buildStore(String storeId, String storeName)
        {
            this.storeId = storeId;
            this.storeName = storeName;
            return this;
        }
        public ReturnVerifyVO buildSales(String salesId, String salesName)
        {
            this.salesId = salesId;
            this.salesName = salesName;
            return this;
        }
        public ReturnVerifyVO buildAssistantIdName(String assistantId, String assistantName)
        {
            this.assistantId = assistantId;
            this.assistantName = assistantName;
            return this;
        }
        public ReturnVerifyVO buildAssistantMnemonic(String assistantMnemonic)
        {
            this.assistantMnemonic = assistantMnemonic;
            return this;
        }
        public ReturnVerifyVO buildAssistant(String assistantId, String assistantName, String assistantMnemonic)
        {
            this.assistantId = assistantId;
            this.assistantName = assistantName;
            this.assistantMnemonic = assistantMnemonic;
            return this;
        }
        public Boolean build判空()
        {
            if (String.IsNullOrEmpty(this.storeId)
                || String.IsNullOrEmpty(this.assistantId))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public void clearStore()
        {
            this.storeId = "";
            this.storeName = "";
        }
    }
}
